package com.shrct.businessprocess.project.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.project.RCCostShareYearConfig;
import com.shrct.businessprocess.project.RCCostShareYearConfigItem;
import com.shrct.businessprocess.project.impl.RCCostShareYearConfigItemImpl;
import com.shrct.businessprocess.project.proxy.RCCostShareYearConfigProxy;
import com.uniquesoft.uidl.validation.Validates;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCCOSTSHAREYEARCONFIG_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rccostshareyearconfig")
public class RCCostShareYearConfigImpl extends BaseModelObject implements RCCostShareYearConfig {
  public RCCostShareYearConfigImpl() {
    super();
  }
  
  private Integer year;
  
  public Integer getYear() {
    return this.year;
  }
  
  public RCCostShareYearConfig setYear(final Integer year) {
    this.year = year;
    return this;			
    
  }
  
  @OneToMany(targetEntity = RCCostShareYearConfigItemImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "config")
  private Collection<RCCostShareYearConfigItem> items = new java.util.ArrayList<com.shrct.businessprocess.project.RCCostShareYearConfigItem>();;
  
  public Collection<RCCostShareYearConfigItem> getItems() {
    return this.items;
  }
  
  public RCCostShareYearConfigItem createAndAddtoItems() {
    getItems().size();
    com.shrct.businessprocess.project.RCCostShareYearConfigItem rCCostShareYearConfigItem = new com.shrct.businessprocess.project.impl.RCCostShareYearConfigItemImpl(this);
    getObjectFactory().create(rCCostShareYearConfigItem);
    getItems().add(rCCostShareYearConfigItem);
    return rCCostShareYearConfigItem;
    
  }
  
  public RCCostShareYearConfigItem createAndAddtoItems(final Procedure1<RCCostShareYearConfigItem> updater) {
    getItems().size();
    com.shrct.businessprocess.project.RCCostShareYearConfigItem rCCostShareYearConfigItem = new com.shrct.businessprocess.project.impl.RCCostShareYearConfigItemImpl(this);
    getObjectFactory().create(rCCostShareYearConfigItem);
    getItems().add(rCCostShareYearConfigItem);
    return rCCostShareYearConfigItem;
    
  }
  
  public RCCostShareYearConfig removeAllItems() {
    for(RCCostShareYearConfigItem obj : this.items){
    	getObjectFactory().delete(obj);
    }
    this.items.clear();
    return this;	
    
  }
  
  public RCCostShareYearConfig removeFromItems(final RCCostShareYearConfigItem rCCostShareYearConfigItem) {
    this.items.remove(rCCostShareYearConfigItem);
    getObjectFactory().delete(rCCostShareYearConfigItem);
    return this;
    
  }
  
  public Double getTotalSharedAmount() {
    final Function1<RCCostShareYearConfigItem,Double> _function = new Function1<RCCostShareYearConfigItem,Double>() {
        public Double apply(final RCCostShareYearConfigItem it) {
          Double _totalCost = it.getTotalCost();
          Double _shareRate = it.getShareRate();
          double _multiply = DoubleExtensions.operator_multiply(_totalCost, _shareRate);
          return Double.valueOf(_multiply);
        }
      };
    Iterable<Double> _map = IterableExtensions.<RCCostShareYearConfigItem, Double>map(this.items, _function);
    final Function2<Double,Double,Double> _function_1 = new Function2<Double,Double,Double>() {
        public Double apply(final Double d1, final Double d2) {
          double _plus = DoubleExtensions.operator_plus(d1, d2);
          return Double.valueOf(_plus);
        }
      };
    Double _reduce = IterableExtensions.<Double>reduce(_map, _function_1);
    Double _nullsafe = NumberExtensions.nullsafe(_reduce);
    return _nullsafe;
  }
  
  public RCCostShareYearConfigItem createItem(final Procedure1<? super RCCostShareYearConfigItem> init) {
    RCCostShareYearConfigItem _createAndAddtoItems = this.createAndAddtoItems();
    final Procedure1<RCCostShareYearConfigItem> _function = new Procedure1<RCCostShareYearConfigItem>() {
        public void apply(final RCCostShareYearConfigItem item) {
          init.apply(item);
          RCCostShareYearConfigImpl.this.checkValid();
        }
      };
    RCCostShareYearConfigItem _doubleArrow = ObjectExtensions.<RCCostShareYearConfigItem>operator_doubleArrow(_createAndAddtoItems, _function);
    return _doubleArrow;
  }
  
  public void deleteItem(final RCCostShareYearConfigItem item) {
    this.removeFromItems(item);
  }
  
  public void checkValid() {
    boolean _notEquals = (!Objects.equal(this.year, null));
    Validates.Assert(_notEquals, "\u5E74\u5EA6\u4E0D\u80FD\u4E3A\u7A7A");
    IGenericQuery<Long> query=getObjectFactory().createGenericQuery(Long.class,"select count(*) from com.shrct.businessprocess.project.impl.RCCostShareYearConfigImpl as e   where ((e.year) = (:year)) and  e.active = 1      ").setParameter("year",year);
    Long _uniqueResult = query.uniqueResult();
    boolean _equals = ((_uniqueResult).longValue() == 1);
    Validates.Assert(_equals, "\u5E74\u5EA6\u5FC5\u987B\u552F\u4E00");
  }
  
  public static RCCostShareYearConfig create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.project.RCCostShareYearConfig rCCostShareYearConfig = new com.shrct.businessprocess.project.impl.RCCostShareYearConfigImpl(
    );
    objectFactory.create(rCCostShareYearConfig);
    return rCCostShareYearConfig;			
    
  }
  
  public static RCCostShareYearConfig createRCCostShareYearConfigImpl(final IObjectFactory objectFactory, final Procedure1<RCCostShareYearConfig> init) {
    com.shrct.businessprocess.project.RCCostShareYearConfig rCCostShareYearConfig = new com.shrct.businessprocess.project.impl.RCCostShareYearConfigImpl(
    );
    objectFactory.create(rCCostShareYearConfig);
    init.apply(rCCostShareYearConfig);
    
    return rCCostShareYearConfig;			
    
  }
  
  public RCCostShareYearConfigProxy toProxy() {
    com.shrct.businessprocess.project.proxy.RCCostShareYearConfigProxy proxy = new com.shrct.businessprocess.project.proxy.RCCostShareYearConfigProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
